M = 10000;
n = 100;
arrayWOccurances = zeros(n);
arraySOccurances = zeros(n);
arrayZOcurrances = zeros(n);

for i = 1:M
    
[W, S, Z] = Qe(n);

arrayWOccurances(W+1) = arrayWOccurances(W+1) + 1;
arraySOccurances(S+1) = arraySOccurances(S+1) + 1;
arrayZOcurrances(Z+1) = arrayZOcurrances(Z+1) + 1;

end

arrayWOccurances = arrayWOccurances./M;
arraySOccurances = arraySOccurances./M;
arrayZOcurrances = arrayZOcurrances./M;

cdfOnesEmpirical = zeros(102);
for i= 2:102
    cdfOnesEmpirical(i) = cdfOnesEmpirical(i-1) + arrayWOccurances(i-1);
end;

%a
n = 100;
probOneRecieve = 0.33;
cdfOnes = 0;

figure(1);
hold on;
title('True (blue) Vs Empirical (red) PMFs of ones received');
axis([0 n 0 0.1]);

figure(2);
hold on;
title('True (blue) vs Empirical (red) CDFs of ones received');
axis([0 n 0 1]);

for i = 0:n
    pmfOnes = nchk(n,i)*(probOneRecieve^i)*((1-probOneRecieve)^(n-i));
    cdfOnes = cdfOnes + pmfOnes;
    figure(1);
    f1 = stem(i,pmfOnes);
    figure(2);
    f2 = plot(i,cdfOnes, '*');
end;


%b)
n = 100;
probBitSwap = 0.05;

figure(3);
hold on;
title('True (blue) vs Empirical (red) PMFs of Bit Swaps');
axis([0 n 0 0.2]);

for i = 0:n
    pmfBitSwapTrue = nchk(n,i)*(probBitSwap^i)*((1-probBitSwap)^(n-i));
    stem(i,pmfBitSwapTrue);
end;

figure(1);
stem(0:size(arrayWOccurances)-1,arrayWOccurances(:,1),'r');
figure(2);
plot(0:size(cdfOnesEmpirical)-1,cdfOnesEmpirical(:,1),'r');
axis([0 100 0 1]);
figure(3);
stem(0:size(arraySOccurances)-1,arraySOccurances(:,1),'r');
